﻿namespace Tomato.Allure
{
	internal class WindowImpl : Tomato.Allure.IWindow
	{
		private EnvDTE80.Window2 m_instance;

		public string Caption
		{
			get { return m_instance.Caption; }
		}

		public Tomato.Allure.IDocument Document
		{
			get
			{
				if( m_instance.Document != null )
				{
					return new DocumentImpl( m_instance.Document );
				}

				return null;
			}
		}

		public int Width
		{
			get { return m_instance.Width; }
		}

		public int Height
		{
			get { return m_instance.Height; }
		}

		public int Top
		{
			get { return m_instance.Top; }
		}

		public int Left
		{
			get { return m_instance.Left; }
		}

		public string Kind
		{
			get { return m_instance.Kind; }
		}

		public bool IsFloating
		{
			get { return m_instance.IsFloating; }
		}

		public bool IsAutoHide
		{
			get { return m_instance.AutoHides; }
		}

		public bool IsVisible
		{
			get { return m_instance.Visible; }
		}

		public bool IsLinkable
		{
			get { return m_instance.Linkable; }
		}

		public Tomato.Allure.IWindow LinkedWindowFrame
		{
			get
			{
				if( m_instance.LinkedWindowFrame != null )
				{
					return new WindowImpl( m_instance.LinkedWindowFrame );
				}

				return null;
			}
		}

		public Tomato.Allure.IProject Project
		{
			get
			{
				if( m_instance.Project != null )
				{
					return new ProjectImpl( m_instance.Project );
				}

				return null;
			}
		}

		public Tomato.Allure.IProjectItem ProjectItem
		{
			get
			{
				if( m_instance.ProjectItem != null )
				{
					return new ProjectItemImpl( m_instance.ProjectItem );
				}

				return null;
			}
		}

		public Tomato.Allure.WindowState WindowState
		{
			get
			{
				switch( m_instance.WindowState )
				{
					case EnvDTE.vsWindowState.vsWindowStateMaximize: return Tomato.Allure.WindowState.Maximized;
					case EnvDTE.vsWindowState.vsWindowStateMinimize: return Tomato.Allure.WindowState.Minimized;
				}

				return Tomato.Allure.WindowState.Normal;
			}
		}

		public Tomato.Allure.WindowType WindowType
		{
			get
			{
				switch( m_instance.Type )
				{
					case EnvDTE.vsWindowType.vsWindowTypeAutos: return WindowType.Autos;
					case EnvDTE.vsWindowType.vsWindowTypeBrowser: return WindowType.Browser;
					case EnvDTE.vsWindowType.vsWindowTypeCallStack: return WindowType.CallStack;
					case EnvDTE.vsWindowType.vsWindowTypeCodeWindow: return WindowType.CodeWindow;
					case EnvDTE.vsWindowType.vsWindowTypeColorPalette: return WindowType.ColorPalette;
					case EnvDTE.vsWindowType.vsWindowTypeDesigner: return WindowType.Designer;
					case EnvDTE.vsWindowType.vsWindowTypeDocument: return WindowType.Document;
					case EnvDTE.vsWindowType.vsWindowTypeDocumentOutline: return WindowType.DocumentOutline;
					case EnvDTE.vsWindowType.vsWindowTypeFind: return WindowType.Find;
					case EnvDTE.vsWindowType.vsWindowTypeFindReplace: return WindowType.FindReplace;
					case EnvDTE.vsWindowType.vsWindowTypeImmediate: return WindowType.Immediate;
					case EnvDTE.vsWindowType.vsWindowTypeLinkedWindowFrame: return WindowType.LinkedWindowFrame;
					case EnvDTE.vsWindowType.vsWindowTypeLocals: return WindowType.Locals;
					case EnvDTE.vsWindowType.vsWindowTypeMainWindow: return WindowType.MainWindow;
					case EnvDTE.vsWindowType.vsWindowTypeOutput: return WindowType.Output;
					case EnvDTE.vsWindowType.vsWindowTypePreview: return WindowType.Preview;
					case EnvDTE.vsWindowType.vsWindowTypeProperties: return WindowType.Properties;
					case EnvDTE.vsWindowType.vsWindowTypeRunningDocuments: return WindowType.RunningDocuments;
					case EnvDTE.vsWindowType.vsWindowTypeSolutionExplorer: return WindowType.SolutionExplorer;
					case EnvDTE.vsWindowType.vsWindowTypeTaskList: return WindowType.TaskList;
					case EnvDTE.vsWindowType.vsWindowTypeThreads: return WindowType.Threads;
					case EnvDTE.vsWindowType.vsWindowTypeToolbox: return WindowType.Toolbox;
					case EnvDTE.vsWindowType.vsWindowTypeToolWindow: return WindowType.ToolWindow;
					case EnvDTE.vsWindowType.vsWindowTypeWatch: return WindowType.Watch;
					default: return WindowType.Autos;
				}
			}
		}

		public WindowImpl( EnvDTE.Window instance )
		{
			m_instance = ( EnvDTE80.Window2 )( instance );
		}

		public void Activate()
		{
			m_instance.Activate();
		}

		public void Close( Tomato.Allure.SaveChanges saveChanges )
		{
			EnvDTE.vsSaveChanges closeOption = EnvDTE.vsSaveChanges.vsSaveChangesNo;
			switch( saveChanges )
			{
				//case Allure.SaveChanges.No: closeOption = vsSaveChanges.vsSaveChangesNo; break;
				case Tomato.Allure.SaveChanges.Prompt: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesPrompt; break;
				case Tomato.Allure.SaveChanges.Yes: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesYes; break;
			}

			m_instance.Close( closeOption ); 
		}

		public void SetFocus()
		{
			m_instance.SetFocus();
		}

		public void SetKind( Tomato.Allure.WindowType windowType )
		{
			EnvDTE.vsWindowType type = EnvDTE.vsWindowType.vsWindowTypeAutos;
			switch( windowType )
			{
				//case Allure.WindowType.Autos: type = vsWindowType.vsWindowTypeAutos; break;
				case Tomato.Allure.WindowType.Browser: type = EnvDTE.vsWindowType.vsWindowTypeBrowser; break;
				case Tomato.Allure.WindowType.CallStack: type = EnvDTE.vsWindowType.vsWindowTypeCallStack; break;
				case Tomato.Allure.WindowType.CodeWindow: type = EnvDTE.vsWindowType.vsWindowTypeCodeWindow; break;
				case Tomato.Allure.WindowType.ColorPalette: type = EnvDTE.vsWindowType.vsWindowTypeColorPalette; break;
				case Tomato.Allure.WindowType.Designer: type = EnvDTE.vsWindowType.vsWindowTypeDesigner; break;
				case Tomato.Allure.WindowType.Document: type = EnvDTE.vsWindowType.vsWindowTypeDocument; break;
				case Tomato.Allure.WindowType.DocumentOutline: type = EnvDTE.vsWindowType.vsWindowTypeDocumentOutline; break;
				case Tomato.Allure.WindowType.Find: type = EnvDTE.vsWindowType.vsWindowTypeFind; break;
				case Tomato.Allure.WindowType.FindReplace: type = EnvDTE.vsWindowType.vsWindowTypeFindReplace; break;
				case Tomato.Allure.WindowType.Immediate: type = EnvDTE.vsWindowType.vsWindowTypeImmediate; break;
				case Tomato.Allure.WindowType.LinkedWindowFrame: type = EnvDTE.vsWindowType.vsWindowTypeLinkedWindowFrame; break;
				case Tomato.Allure.WindowType.Locals: type = EnvDTE.vsWindowType.vsWindowTypeLocals; break;
				case Tomato.Allure.WindowType.MainWindow: type = EnvDTE.vsWindowType.vsWindowTypeMainWindow; break;
				case Tomato.Allure.WindowType.Output: type = EnvDTE.vsWindowType.vsWindowTypeOutput; break;
				case Tomato.Allure.WindowType.Preview: type = EnvDTE.vsWindowType.vsWindowTypePreview; break;
				case Tomato.Allure.WindowType.Properties: type = EnvDTE.vsWindowType.vsWindowTypeProperties; break;
				case Tomato.Allure.WindowType.RunningDocuments: type = EnvDTE.vsWindowType.vsWindowTypeRunningDocuments; break;
				case Tomato.Allure.WindowType.SolutionExplorer: type = EnvDTE.vsWindowType.vsWindowTypeSolutionExplorer; break;
				case Tomato.Allure.WindowType.TaskList: type = EnvDTE.vsWindowType.vsWindowTypeTaskList; break;
				case Tomato.Allure.WindowType.Threads: type = EnvDTE.vsWindowType.vsWindowTypeThreads; break;
				case Tomato.Allure.WindowType.Toolbox: type = EnvDTE.vsWindowType.vsWindowTypeToolbox; break;
				case Tomato.Allure.WindowType.ToolWindow: type = EnvDTE.vsWindowType.vsWindowTypeToolWindow; break;
				case Tomato.Allure.WindowType.Watch: type = EnvDTE.vsWindowType.vsWindowTypeWatch; break;
			}

			m_instance.SetKind( type ); 
		}

	}
}
